package com.sks.web.action.deployment;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.sks.bean.pojo.Area;
import com.sks.service.house.AreaService;
import com.sks.web.action.BasicAction;
import com.sks.web.formbean.house.AreaForm;

import flexjson.JSONSerializer;

@Controller("/sks/frontpages/privilege/deployment/subarea")
public class GetSubAreaAction extends BasicAction {
	@Resource(name = "areaServiceBean")
	private AreaService service;

	/**
	 * 区域二级联动
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getAreasByTopId(ActionMapping mapping, ActionForm form,
         HttpServletRequest request, HttpServletResponse response) throws Exception {
	   AreaForm formbean = (AreaForm) form;
	   if (null == formbean.getAreaId() || 0 == formbean.getAreaId()) {
		   response.getWriter().write("[{\"name\":\"\",\"areaId\":\"\"}]");
		   return null;
	   }
	   Area topArea = service.find(formbean.getAreaId());
	   List<Area> areas = topArea.getAreas();
	   JSONSerializer json = new JSONSerializer();
	   String jsonStr = json.exclude("*.class", "areas", "parentArea", "visible", "remark").include("areaId", "name").serialize(areas);
      response.getWriter().write(jsonStr);
      return null;
   }
	
}
